package com.gopay.bis.hnapay.ebank.operation;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.hnapay.ebank.query.HnaPayMessageQuery;
import com.gopay.common.util.KeyUtil;

/**
 * 新生在线网银直连接口查询处理
 * @author LiLiLi
 *
 */
public class HnaPayCertHelperQuery {
	private String bocKey = null;
	private String commKey = null;
	private String cebKey = null;
	private String sdbKey = null;
	private String gdbKey = null;
	private String cibKey = null;
	private String eciticKey = null;
	private String abcKey = null;
	private String cmbKey = null;
	private String bocParterID = null;
	private String commParterID = null;
	private String cebParterID = null;
	private String sdbParterID = null;
	private String gdbParterID = null;
	private String cibParterID = null;
	private String eciticParterID = null;
	private String abcParterID = null;
	private String cmbParterID = null;
	private final static String key = "506ff436";

	/**
	 * 构建请求订单查询信息
	 * 
	 * @param message
	 * @return
	 */
	public Map<String, String> buildQueryMsg(HnaPayMessageQuery message) {
		Map<String, String> param = new HashMap<String, String>();
		param.put("version", message.getVersion());
		param.put("serialID", message.getSerialID());
		param.put("mode", message.getMode());
		param.put("type", message.getQueryType());
		param.put("orderID", message.getBankOrderId());
		param.put("beginTime", message.getBeginTime());
		param.put("endTime", message.getEndTime());
		param.put("partnerID", message.getPartnerID());
		param.put("remark", message.getRemark());
		param.put("charset", message.getCharset());
		param.put("signType", message.getSignType());
		param.put("signMsg", message.getSignMsg());
		BankLogUtils.printB2CQuerySendLog(message, param);
		return param;
	}

	
	/**
	 * 解析订单查询返回对象
	 * 
	 * @param obj
	 * @return
	 */
	public HnaPayMessageQuery parseQueryMsg(Object obj){
		// 解析订单查询返回对象
				HnaPayMessageQuery messageQuery = new HnaPayMessageQuery();
				messageQuery.setOutStlmCd(BankConstants.BANK_LOGO_HNAPAY);
				// 解析返回信息
				String reciverDate = "";
				if (obj instanceof byte[]) {
					reciverDate = new String((byte[]) obj);
				} else if (obj instanceof String) {
					reciverDate = (String) obj;
				}
				BankLogUtils.printB2CQueryBlackLog(messageQuery, reciverDate);

				/*
				 * 返回的数据stream格式为： Key1=value1 加换行符：[\n] Key2=value2 加换行符：[\n]
				 */
				String[] ls = reciverDate.split("&");

				for (int i = 0; i < ls.length; i++) {
					String currentResult = (String) ls[i];
					if (currentResult == null || currentResult.equals("")) {
						continue;
					}
					int j = currentResult.indexOf("=");
					if (j >= 0) {
						String sKey = currentResult.substring(0, j);
						String sValue = currentResult.substring(j + 1);
						try {
							sValue = URLDecoder.decode(sValue, "GBK");
						} catch (UnsupportedEncodingException e) {
							BankLogUtils.printB2CQueryExceptionLog(messageQuery, e);
							throw new RuntimeException(e.getMessage());
						}
						 if(!reciverDate.contains("ErrorCode")){
							  if(sKey.equals("serialID")){
									messageQuery.setSerialID(sValue);
								}else if(sKey.equals("mode")){
									messageQuery.setMode(sValue);
								}else if(sKey.equals("type")){
									messageQuery.setQueryType(sValue);
								}else if(sKey.equals("partnerID")){
									messageQuery.setPartnerID(sValue);
								}else if(sKey.equals("remark")){
									messageQuery.setRemark(sValue);
								}else if(sKey.equals("charset")){
									messageQuery.setCharset(sValue);
								}else if(sKey.equals("signType")){
									messageQuery.setSignType(sValue);
								} else if (sKey.equals("signMsg")) {
									messageQuery.setSignMsg(sValue);
								}else if (sKey.equals("resultCode")) {
								messageQuery.setResultCode(sValue);
								}else if(sKey.equals("queryDetailsSize")){
									messageQuery.setQueryDetailsSize(sValue);
								}else if (sKey.equals("queryDetails")) {
									messageQuery.setQueryDetails(sValue);
								}
						 }else{
							 if (sKey.equals("ErrorCode")) {
									messageQuery.setResultCode(sValue);
							  }
						 }
					}
				}
				return messageQuery;
	}
	public String buildPlain(HnaPayMessageQuery message) {
		StringBuffer sb = new StringBuffer("");
		String pkey = "";
		try{
			if(message.getPartnerID().equals(bocParterID)){
				pkey=KeyUtil.decryptDES(bocKey, key);
			}else if(message.getPartnerID().equals(commParterID)){
				pkey=KeyUtil.decryptDES(commKey, key);
			}else if(message.getPartnerID().equals(cebParterID)){
				pkey=KeyUtil.decryptDES(cebKey, key);
			}else if(message.getPartnerID().equals(sdbParterID)){
				pkey=KeyUtil.decryptDES(sdbKey, key);
			}else if(message.getPartnerID().equals(cibParterID)){
				pkey=KeyUtil.decryptDES(cibKey, key);
			}else if(message.getPartnerID().equals(gdbParterID)){
				pkey=KeyUtil.decryptDES(gdbKey, key);
			}else if(message.getPartnerID().equals(gdbParterID)){
				pkey=KeyUtil.decryptDES(gdbKey, key);
			}else if(message.getPartnerID().equals(eciticParterID)){
				pkey=KeyUtil.decryptDES(eciticKey, key);
			}else if(message.getPartnerID().equals(abcParterID)){
				pkey=KeyUtil.decryptDES(abcKey, key);
			}else if(message.getPartnerID().equals(cmbParterID)){
				pkey=KeyUtil.decryptDES(cmbKey, key);
			}else{
				BankLogUtils.printB2cQueryErrorLog(message, "HNAPay query parterID not matches!");
			}
		}catch(Exception e){
			BankLogUtils.printB2CQueryExceptionLog(message, e);
		}
		sb.append("serialID=").append(message.getSerialID());
		sb.append("&mode=").append(
				message.getMode());
		sb.append("&type=").append(message.getQueryType());
		sb.append("&resultCode=").append(message.getResultCode());
		sb.append("&queryDetailsSize=").append(message.getQueryDetailsSize());
		sb.append("&queryDetails=").append(message.getQueryDetails());
		sb.append("&partnerID=").append(message.getPartnerID());
		sb.append("&remark=").append(message.getRemark());
		sb.append("&charset=").append(message.getCharset());
		sb.append("&signType=").append(message.getSignType());
		sb.append("&pkey=").append(pkey);
		return sb.toString();
	}
	public void setBocKey(String bocKey) {
		this.bocKey = bocKey;
	}

	public void setCommKey(String commKey) {
		this.commKey = commKey;
	}

	public void setCebKey(String cebKey) {
		this.cebKey = cebKey;
	}

	public void setSdbKey(String sdbKey) {
		this.sdbKey = sdbKey;
	}

	public void setGdbKey(String gdbKey) {
		this.gdbKey = gdbKey;
	}

	public void setCibKey(String cibKey) {
		this.cibKey = cibKey;
	}


	public void setBocParterID(String bocParterID) {
		this.bocParterID = bocParterID;
	}


	public void setCommParterID(String commParterID) {
		this.commParterID = commParterID;
	}


	public void setCebParterID(String cebParterID) {
		this.cebParterID = cebParterID;
	}


	public void setSdbParterID(String sdbParterID) {
		this.sdbParterID = sdbParterID;
	}


	public void setGdbParterID(String gdbParterID) {
		this.gdbParterID = gdbParterID;
	}


	public void setCibParterID(String cibParterID) {
		this.cibParterID = cibParterID;
	}


	public void setEciticKey(String eciticKey) {
		this.eciticKey = eciticKey;
	}


	public void setEciticParterID(String eciticParterID) {
		this.eciticParterID = eciticParterID;
	}


	public void setAbcKey(String abcKey) {
		this.abcKey = abcKey;
	}


	public void setAbcParterID(String abcParterID) {
		this.abcParterID = abcParterID;
	}


	public void setCmbKey(String cmbKey) {
		this.cmbKey = cmbKey;
	}


	public void setCmbParterID(String cmbParterID) {
		this.cmbParterID = cmbParterID;
	}
	
}
